*************************************************************************
*																		*
*					Campaign Panel Book Chapter 6 - Michigan Variables	*
*																		*
*************************************************************************	

use "${data}\ZA5704_en_v3-2-0.dta", clear


********************************************************************************
** East-West
********************************************************************************

tab ostwest, nol
gen ost1=0
replace ost1=1 if ostwest==2
tab1 ost1*

foreach welle of numlist 2/7 {
	gen ost`welle'=ost1
}  	

tab1 ost* 
 

******************************************************************************
**																			**
**			Reasonable party-identification variable						**
**																			**
******************************************************************************


recode kp1_2090a (2/3=1 "CDU/CSU") (4=4 "SPD") ///
	(5=5 "FDP") (6=6 "Greens") (7=7 "The Left") (801=801 "andere Partei") ///
	(808=808 "no party") (-99=.), gen(pidfull1)

fre pidfull1

   
 
 
********************************************************************************
**
** Coalition evaluation scales
**
********************************************************************************   

tab1 kp2_910a kp2_961a
   
foreach welle of numlist 2 4 6 7{
      // Coalition: Desirable
      recode kp`welle'_910a (-99/-1 = .), gen(hkoagk`welle')
      recode kp`welle'_910b (-99/-1 = .), gen(hkoasge`welle')
      recode kp`welle'_910c (-99/-1 = .), gen(hkoarg`welle')
      recode kp`welle'_910e (-99/-1 = .), gen(hkoasgr`welle')
      recode kp`welle'_910h (-99/-1 = .), gen(hkoarrg`welle')
	recode kp`welle'_910f (-99/-1 = .) , gen(hkoaamp`welle') //ampel
}	  

// Replace missing values in wave 3 and 5 by mean value

foreach welle of numlist 3 5 {
   foreach var in hkoagk hkoasge hkoarg hkoasgr hkoarrg hkoaamp  {
	     local prev = `welle'-1
		 local next = `welle'+1
		 egen `var'`welle' = rowmean(`var'`prev' `var'`next')
	  }
}

// Replace missing values at the sides by successor/predecessor

foreach var in hkoagk hkoasge hkoarg hkoasgr hkoarrg hkoaamp {
      gen `var'1 = `var'2 
 }


// Generate coalition dummies to achieve measures consistent with 2009

* Determine rank. Best evaluation = rank 1, missing is assigned missing rank
foreach welle of numlist 1/7 {
   rowranks hkoasgr`welle' hkoagk`welle' hkoasge`welle' hkoarg`welle' ///
      hkoarrg`welle' hkoaamp`welle' , gen(rkoasgr`welle' rkoagk`welle' rkoasge`welle' ///
      rkoarg`welle' rkoarrg`welle' rkoaamp`welle') descending highmissing
}

* Save rank values
foreach welle of numlist 1/7 {
 rowsort hkoasgr`welle' hkoagk`welle' hkoasge`welle' hkoarg`welle' ///
      hkoarrg`welle' hkoaamp`welle', gen(vrang1_`welle' vrang2_`welle' ///
		vrang3_`welle' vrang4_`welle' vrang5_`welle' vrang6_`welle') descending highmissing
}



list hkoa*4 rkoa*4 vrang*4 in 1/50
 
 
/* Rules for coalition dummy:
Coalition preference dummy for a specific coalition = 1 if the coalition received
the best evaluation by the respondent and this evaluation exceeds the mean value, 
meaning it is greater than 6

If a respondent evaluates all coalitions as worse than 6, she does not possess
a preference for any coalition

If a respondent evaluates more than one coalition with the same value which is 
also larger than six, the respondent gets a 1 for more than one coalition

*/
 

foreach welle of numlist 1/7 {
   foreach koal in koasge koagk koarg koarrg {
      gen `koal'`welle' = .
	  replace `koal'`welle' = 0 if r`koal'`welle' > 1 & r`koal'`welle'!=.
	  replace `koal'`welle' = 1 if r`koal'`welle' == 1
	  replace `koal'`welle' = 0 if r`koal'`welle' == 1 & h`koal'`welle'<7
	  replace `koal'`welle' = 1 if r`koal'`welle'!=1 & /// 
		h`koal'`welle'==vrang1_`welle' & vrang1_`welle'>=7 & vrang1_`welle'!=.
}
}


// Control 

list hkoa*2 koa*2 in 1/50

egen countkoalitions4 = anycount(koa*4), values(1)
replace countkoalitions = . if hkoasge4==. & hkoagk4==. & ///
	hkoarg4==. & hkoarrg4==.
fre countkoalitions4  //few missings
fre w4b //this works

fre hkoa*4

* Average coalition evaluation when a preference exists according to our coding
* scheme. If no preference for a coalition exists, the assigned value cannot be
* 11, this is logically impossible. 
* Otherwise there must be a mistake in the code. 

bysort koasge4: sum hkoasge4 //wenn koasge4=0 ist, darf hkoasge nicht gleich 11 sein
bysort koarrg4: sum hkoarrg4

* Are there problems with particular missings in a row?
* Probleme mit einzelnen Missings in der Reihe?
list lfdn hkoa*4 rko*4 vrang*4 if hkoagk4==. & hkoasge4==. & hkoarg4!=.
//everything is fine


********************************************************************************
**
** Satisfaction with the government
**
******************************************************************************** 
 
foreach welle of numlist 1/7 {
   recode kp`welle'_730 (-99/-93=.), gen(regzufr`welle') copyrest
   replace regzufr`welle'=(regzufr`welle'-1)/10
}

tab regzufr5
 
********************************************************************************
**
** Evaluation scales candidates
**
********************************************************************************

numlabel kp1_650a, add
tab kp1_650a

foreach welle of numlist 1/7 {
   recode kp`welle'_650a (-99 -93 -95 -92= .) (-71=6), gen(kand1`welle') copyrest 
   recode kp`welle'_650k (-99 -93 -95 -92= .) (-71=6), gen(kand4`welle') copyrest
   recode kp`welle'_650o (-99 -93 -95 -92= .) (-71=6), gen(kand5`welle') copyrest
   recode kp`welle'_650h (-99 -93 -95 -92= .) (-71=6), gen(kand6`welle') copyrest
   recode kp`welle'_650j (-99 -93 -95 -92= .) (-71=6), gen(kand7`welle') copyrest
 } 
 
  // value range

foreach welle of numlist 1/7 {
   replace kand1`welle'=(kand1`welle'-1)/10
   replace kand4`welle'=(kand4`welle'-1)/10
   replace kand5`welle'=(kand5`welle'-1)/10
   replace kand6`welle'=(kand6`welle'-1)/10
   replace kand7`welle'=(kand7`welle'-1)/10
   }

sum kand?6
 
********************************************************************************
**
** Competence Grading
**
********************************************************************************

/* Competence grade for a party = 0 
if another party was named,
if no party was named,
if no problem was named. 

kp?_850b most important problem
kp?_870b second most important problem
*/


foreach welle of numlist 1/7 {
foreach partei of numlist 1 4/7 {
recode kp`welle'_850b (-99 -93 -95=.) (-97=0) (`partei'=1) ///
	(else=0), gen(first`partei'`welle')
recode kp`welle'_870b (-99 -93 -95=.) (-97=0) (`partei'=1) ///
	(else=0), gen(second`partei'`welle')
}
}


tab kp7_850b first47, m

foreach welle of numlist 1/7 {
foreach partei of numlist 1 4/7 {
gen komp`partei'`welle' = 0 if first`partei'`welle'==0 & ///
	second`partei'`welle'==0
replace komp`partei'`welle' = 0.33 if first`partei'`welle'==0 & ///
	second`partei'`welle'==1
replace komp`partei'`welle' = 0.66 if first`partei'`welle'==1 & ///
	second`partei'`welle'==0
replace komp`partei'`welle' = 1 if first`partei'`welle'==1 & ///
	second`partei'`welle'==1
}
}

list first44 second44 komp44 if first44==. & second44~=. //this is not problematic
list first44 second44 komp44 if first44~=. & second44==. //this might be problematic

sum komp*


fre kp4_860_c1 if first44~=. & second44==. //a second most important problem was mentioned
fre kp4_870b if first44~=. & second44==. // information for solution competence was refused
//Missing in komp44 is ok in this case

drop first*
drop second*


	  
********************************************************************************
**
** Economic Voting
**
********************************************************************************

// Individual economic situation and general economic situation (in retrospect)

foreach welle of numlist 1 4 7 {
   recode kp`welle'_760 (1=5) (2=4) (3=3) (4=2) (5=1) (-99/-92=.), ///
      gen(ewl`welle')
   recode kp`welle'_800 (1=5) (2=4) (3=3) (4=2) (5=1) (-99/-92=.), ///
      gen(awl`welle')
}


// Replacing missing values in waves 2, 3, 5, 6
// weighted mean from existing waves, weight dependent on time lag

foreach welle of numlist 2 3 5 6 {
   foreach var in awl ewl  {
      gen `var'`welle' = . 
}
}   

foreach var in awl ewl {
   
   replace `var'2 = 2/3*`var'1 + 1/3*`var'4
   replace `var'3 = 1/3*`var'1 + 2/3*`var'4
   replace `var'5 = 2/3*`var'4 + 1/3*`var'7
   replace `var'6 = 1/3*`var'4 + 2/3*`var'7
   
}


foreach welle of numlist 1/7 {
   replace awl`welle'=(awl`welle'-1)/4
   replace ewl`welle'=(ewl`welle'-1)/4
   }
   
tab1 awl2 ewl2 

list kp?_760 ewl1 ewl2 ewl3 ewl4 ewl5 ewl6 ewl7  //stimmt

********************************************************************************
**
** Save the relevant variables
**
********************************************************************************

keep lfdn ost* kand* komp* ewl? awl? regzufr? ///
   koa* pid*

save "Data\michigan13.dta", replace
   